<?php

namespace app\api\controller;

use app\api\controller\Common;
use app\api\model\Common as CommonMx;
use think\Db;

class Goldcoin extends Common
{

    /**
     * 充值列表
     */
    public function getList()
    {
        $tokenRes = $this->checkToken();
//        if ($tokenRes['status'] == 400) {
//            datamsg(400, $tokenRes['mess'], $tokenRes['data']);
//        } else {
//            $userId = $tokenRes['user_id'];
//        }
        $ratio = db('live_config')->where(['id'=>1])->find();
        if (!$ratio || !$ratio['gold_coin_to_rmb']) {
            datamsg(400, '缺少充值比例', array('status' => 400));
        }

        $list = Db::name('gold_coin_recharge')->select();
        foreach ($list as $key => $row) {
            $goldCoin = $row['money'] * $ratio['gold_coin_to_rmb'];
            $list[$key]['gold_coin'] = (string)$goldCoin;
        }
        datamsg(200, '获取充值列表成功', $list);
    }

    /**
     * 添加订单
     */
    public function addOrder()
    {
        $tokenRes = $this->checkToken();
        if ($tokenRes['status'] == 400) {
            datamsg(400, $tokenRes['mess'], $tokenRes['data']);
        } else {
            $userId = $tokenRes['user_id'];
        }
        if (!input('post.recharge_id')) {
            datamsg(400, '缺少充值ID', array('status' => 400));
        }
        $ratio = db('live_config')->where(['id'=>1])->find();
        if (!$ratio || !$ratio['gold_coin_to_rmb']) {
            datamsg(400, '缺少充值比例', array('status' => 400));
        }

        $info = Db::name('gold_coin_recharge')->where('deleted', 0)->where('id', input('post.recharge_id'))->find();
        if (!$info) {
            datamsg(400, '充值产品不存在或已下架', array('status' => 400));
        }

        $data['user_id'] = $userId;
        $order_number = $orderNumber = 'G' . date('YmdHis') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $data['order_number'] = $order_number;
        $data['recharge_id'] = input('post.recharge_id');
        $data['money'] = $info['money'];
        $goldCoin = $info['money'] * $ratio['gold_coin_to_rmb'];
        $data['gold_coin'] = (string)$goldCoin;
        $data['ratio'] = (string)$ratio['gold_coin_to_rmb'];
        $data['created'] = date('Y-m-d H:i:s');
        $data['updated'] = date('Y-m-d H:i:s');
        $data['time_out'] = date('Y-m-d H:i:s', time() + 300);
        if (Db::name('gold_coin_recharge_order')->insert($data)) {
            datamsg(200, '订单创建成功', $data);
        }
    }

    /**
     *  获取充值订单详情
     */
    public function getOrderInfo()
    {
        $tokenRes = $this->checkToken();
        if ($tokenRes['status'] == 400) {
            datamsg(400, $tokenRes['mess'], $tokenRes['data']);
        } else {
            $userId = $tokenRes['user_id'];
        }
        if (!input('post.order_number')) {
            datamsg(400, '缺少订单号', array('status' => 400));
        }
        $orderInfo = Db::name('gold_coin_recharge_order')->where('user_id', $userId)->where('order_number', input('post.order_number'))->find();
        datamsg(200, '订单获取成功', $orderInfo);
    }
}
